package org.hq.client.ws;

import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class OkWebSocket extends WebSocketListener {
    @Override
    public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response) {
        System.out.println("WebSocket open");
    }

    @Override
    public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
        // 接收到文本消息时的回调
        System.out.println("接收到文本消息: " + text);
    }

    @Override
    public void onClosing(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
        // 即将关闭连接时的回调
        webSocket.close(1000, "关闭连接");
    }

    @Override
    public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @Nullable Response response) {
        System.out.println("连接失败时的回调WebSocket failure");
    }

    @Override
    public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
        System.out.println("WebSocket closed");
    }
}
